package com.example.testapp.activity.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testapp.R;
import com.example.testapp.activity.activity.study.WeaterMainActivity;
import com.example.testapp.activity.model.ResponseWrapper;

import java.util.Calendar;
import java.util.List;

/**
 * Created by l on 2016/8/2.
 */
public class LifeIndexFragment extends Fragment {

    public static final String TAG = "TodayCan";

    public List<ResponseWrapper.WeaterEntity.WeatherSubEntity.LivingIndexEntity>  listsib;
    public View baseView;
    public TextView mDescTV;
    public TextView mDateTV;
    public GridView mTodayInfoGrid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        WeaterMainActivity.TAG_H = TAG;
        baseView = inflater.inflate(R.layout.fragment_life_index, container, false);

        initView();
        initData();
        return baseView;
    }

    private void initView() {
        mDescTV = (TextView)baseView.findViewById(R.id.todaycan_dec);
        mDateTV = (TextView)baseView.findViewById(R.id.date_tv);
        mTodayInfoGrid = (GridView)baseView.findViewById(R.id.gridview);
    }

    private void initData() {
         initDate();
         if (WeaterMainActivity.response.getResults() == null){
             Toast.makeText(getActivity(), "获取指数信息失败，请检查网络连接",0).show();
         }else{
             setData();
         }
    }

    private void setData() {
        listsib = WeaterMainActivity.response.getResults().get(0).getIndex();
        if(WeaterMainActivity.response.getResults().get(0).getIndex().toString() == "[]"){

        }else{
            mTodayInfoGrid.setAdapter(new GridTodayAdapter(getActivity(), listsib));
        }
        mTodayInfoGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mDescTV.setText(listsib.get(position).getDes());
            }
        });
    }

    private void initDate() {
        Calendar calendar = Calendar.getInstance();
        int  year = calendar.get(Calendar.YEAR);
        int  month = calendar.get(Calendar.MONTH)+1;
        int  day = calendar.get(Calendar.DAY_OF_MONTH);
        mDateTV.setText(year + "年" + month + "月" + day + "日");
    }

    public class GridTodayAdapter extends BaseAdapter{

        private LayoutInflater inflater;
        private List<ResponseWrapper.WeaterEntity.WeatherSubEntity.LivingIndexEntity>  mListIndex;
        private int[] resours = {
                R.mipmap.ic_todaycan_dress, R.mipmap.ic_todaycan_carwash,
                R.mipmap.ic_todaycan_tour, R.mipmap.ic_todaycan_coldl,
                R.mipmap.ic_todaycan_sport, R.mipmap.ic_todaycan_ultravioletrays,
        };

        public GridTodayAdapter(Context context, List<ResponseWrapper.WeaterEntity.WeatherSubEntity.LivingIndexEntity> listsib) {
            this.inflater = LayoutInflater.from(context);
            this.mListIndex = listsib;
        }

        @Override
        public int getCount() {
            return mListIndex == null ? 0 : mListIndex.size();
        }

        @Override
        public Object getItem(int position) {
            return mListIndex.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.view_grid_todaycan, parent, false);
                holder.dothing = (TextView) convertView.findViewById(R.id.dothing);
                holder.index = (TextView) convertView.findViewById(R.id.index);
                holder.image_index = (ImageView)convertView.findViewById(R.id.image_index);
                holder.image_click = (ImageView)convertView.findViewById(R.id.image_click);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

           if(position == 0){
               holder.dothing.setText("穿衣指数");
           }else if(position == 1){
               holder.dothing.setText("洗车指数");
           }else if(position == 2){
               holder.dothing.setText("旅游指数");
           }else if(position == 3){
               holder.dothing.setText("感冒指数");
           }else if(position == 4){
               holder.dothing.setText("运动指数");
           }else if(position == 5){
               holder.dothing.setText("紫外线指数");
           }else{
               holder.dothing.setText(mListIndex.get(position).getTipt());
           }

            holder.index.setText("点击查看详情");

            Log.i("TAG", mListIndex.size()+"sportIndex.size()");
            holder.image_index.setBackgroundResource(resours[position]);
            holder.image_click.setBackgroundResource(R.mipmap.ic_todaycan_clickbt);
            return convertView;
        }

        public class ViewHolder{
            public TextView dothing;
            public TextView index;
            public ImageView image_index;
            public ImageView image_click;
        }
    }
}
